//
//  MainViewController.swift
//  wobo
//
//  Created by 杨博宇 on 16/3/6.
//  Copyright © 2016年 杨博宇. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    @objc private func plusBtnDidClick() {
        print(__FUNCTION__)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //创建一个自定义MainTabBar
        let mainTabBar = MainTabBar()
        
        mainTabBar.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)
        
        //系统tabbar是 只读属性，不能直接操作。通过kvc访问对象的属性
        setValue(mainTabBar, forKey: "tabBar")
        
        addChildViewControllers()
    }
    
    private func addChildViewControllers() {
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    //MARK: 添加子视图控制器
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
//        let home = HomeViewController()
        //设置title
//        vc.navigationItem.title = title
        vc.title = title
        vc.tabBarItem.title = title
        
        //设置tintColor
//        tabBar.tintColor = UIColor.orangeColor()
        
        //设置图片
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置选中图片
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")//?.imageWithRenderingMode(.AlwaysOriginal)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }

}
